class Solution
{
public:
    vector<int> numSmallerByFrequency(vector<string> &queries, vector<string> &words)
    {
        auto f = [](string &s)
        {
            map<char, int> m;
            for (char c : s)
            {
                ++m[c];
            }
            return m.begin()->second;
        };

        vector<int> fWords;
        for (auto &word : words)
        {
            fWords.push_back(f(word));
        }
        sort(fWords.begin(), fWords.end());

        vector<int> answer;
        for (auto &query : queries)
        {
            answer.push_back(fWords.end() - upper_bound(fWords.begin(), fWords.end(), f(query)));
        }
        return answer;
    }
};